<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="微信open_id" prop="memberAppId" >
        <a-input disabled v-model="form.memberAppId" placeholder="请输入微信open_id" />
      </a-form-model-item>
      <a-form-model-item label="会员学号" prop="numberNum" >
        <a-input disabled v-model="form.numberNum" placeholder="请输入会员学号" />
      </a-form-model-item>
      <a-form-model-item label="会员姓名" prop="memberName" >
        <a-input v-model="form.memberName" placeholder="请输入会员姓名" />
      </a-form-model-item>
      <!--      <a-form-model-item label="会员性别" prop="memberSex" >-->
      <!--        <a-select placeholder="请选择会员性别" v-model="form.memberSex">-->
      <!--          <a-select-option v-for="(d, index) in memberSexOptions" :key="index" :value="d.dictValue" >{{ d.dictLabel }}</a-select-option>-->
      <!--        </a-select>-->
      <!--      </a-form-model-item>-->
      <!--      <a-form-model-item label="会员专业" prop="memberSubject" >-->
      <!--        <a-input v-model="form.memberSubject" placeholder="请输入会员专业" />-->
      <!--      </a-form-model-item>-->
      <!--      <a-form-model-item label="会员班级" prop="memberClass" >-->
      <!--        <a-input v-model="form.memberClass" placeholder="请输入会员班级" />-->
      <!--      </a-form-model-item>-->
      <!--      <a-form-model-item label="会员爱好" prop="memberHobby" >-->
      <!--        <a-input v-model="form.memberHobby" placeholder="请输入会员爱好" />-->
      <!--      </a-form-model-item>-->
      <a-form-model-item label="会员类型" prop="memberType" >
        <a-select placeholder="请选择会员类型" v-model="form.memberType">
          <a-select-option v-for="(d, index) in memberTypeOptions" :key="index" :value="d.dictValue" >{{ d.dictLabel }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="会员服务时长" prop="memberDuration" >
        <a-input v-model="form.memberDuration" placeholder="请输入会员服务时长" />
      </a-form-model-item>

      <!--      <a-form-model-item label="用户头像" prop="memberAvatarUrl" >-->
      <!--        <a-input v-model="form.memberAvatarUrl" placeholder="请输入用户头像" />-->
      <!--      </a-form-model-item>-->
      <!--      <a-form-model-item label="会员昵称" prop="memberNickname" >-->
      <!--        <a-input v-model="form.memberNickname" placeholder="请输入会员昵称" />-->
      <!--      </a-form-model-item>-->
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { getMember, addMember, updateMember } from '@/api/angel/member'

export default {
  name: 'CreateForm',
  props: {
    memberSexOptions: {
      type: Array,
      required: true
    },
    memberTypeOptions: {
      type: Array,
      required: true
    }
  },
  components: {
  },
  data () {
    const validateMemberType = (rule, value, callback) => {
      this.getDicts('member_type').then(response => {
        const resArr = response.data
        const validArr = []
        resArr.forEach((item) => {
          validArr.push(item.dictValue)
        })
        if (!validArr.includes(value)) {
          callback(new Error('用户类型错误'))
        } else {
          callback()
        }
      })
    }
    return {
      loading: false,
      formTitle: '',
      // 表单参数
      form: {
        memberId: null,
        memberAppId: null,
        memberName: null,
        memberSex: null,
        memberSubject: null,
        memberClass: null,
        memberHobby: null,
        memberType: null,
        memberDuration: null,
        numberNum: null,
        memberAvatarUrl: null,
        memberNickname: null
      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
        memberName: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
        ],
        memberDuration: [
          { required: true, message: '时长不能为空', trigger: 'blur' }
        ],
        memberType: [
          { required: true, message: '用户类型不能为空', trigger: 'blur' },
          { validator: validateMemberType, trigger: 'blur' }
        ]
      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        memberId: null,
        memberAppId: null,
        memberName: null,
        memberSex: null,
        memberSubject: null,
        memberClass: null,
        memberHobby: null,
        memberType: null,
        memberDuration: null,
        numberNum: null,
        memberAvatarUrl: null,
        memberNickname: null
      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加活动'
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      this.formType = 2
      const memberId = row ? row.memberId : ids
      getMember(memberId).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改活动'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.memberId !== undefined && this.form.memberId !== null) {
            updateMember(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            })
          } else {
            addMember(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
